package com.funcablaze.func;

import org.lwjgl.BufferUtils;

import java.nio.FloatBuffer;

public class Color {
	public int r, g, b, a = 255;
	
	public Color(int red, int green, int blue, int alpha) {
		this.r = red;
		this.g = green;
		this.b = blue;
		this.a = alpha;
	}
	
	public Color(int red, int green, int blue) {
		this.r = red;
		this.g = green;
		this.b = blue;
	}
	
	public Color(String hex) {
		if (hex.length() == 7) {
			this.r = Integer.parseInt(hex.substring(1, 3), 16);
			this.g = Integer.parseInt(hex.substring(3, 5), 16);
			this.b = Integer.parseInt(hex.substring(5, 7), 16);
		} else if (hex.length() == 4) {
			this.r = Integer.parseInt(hex.substring(1, 2), 16) * 17;
			this.g = Integer.parseInt(hex.substring(2, 3), 16) * 17;
			this.b = Integer.parseInt(hex.substring(3, 4), 16) * 17;
		} else if (hex.length() == 9) {
			this.r = Integer.parseInt(hex.substring(1, 3), 16);
			this.g = Integer.parseInt(hex.substring(3, 5), 16);
			this.b = Integer.parseInt(hex.substring(5, 7), 16);
			this.a = Integer.parseInt(hex.substring(7, 9), 16);
		} else {
			throw new IllegalArgumentException("无效的十六进制颜色：" + hex);
		}
	}
	
	/**
	 * 将颜色转换为FloatBuffer
	 * @return FloatBuffer
	 */
	public FloatBuffer toFloatBuffer() {
		return BufferUtils.createFloatBuffer(4)
					   .put(new float[]{r(), g(), b(), a()})
					   .flip();
	}
	
	public float r() {
		return r / 255f;
	}
	
	public float g() {
		return g / 255f;
	}
	
	public float b() {
		return b / 255f;
	}
	
	public float a() {
		return a / 255f;
	}
}
